from django.shortcuts import render_to_response
from django.template.context import RequestContext
from django.contrib import messages
from django.http import HttpResponseRedirect
from django.views.decorators.http import require_GET
from django.core.urlresolvers import reverse
from django.contrib.auth import logout as auth_logout
import logging

@require_GET
def index(request):
    if request.user.is_authenticated() :
        activity = request.user.workout_set.order_by('date')[:5]
        events = request.user.event_set.all().order_by('date').order_by('time')[:5]
        return render_to_response("index.html", {"activity": activity, "events":events}, context_instance = RequestContext(request));
    else :
        return render_to_response("index.html", context_instance = RequestContext(request));

def logout(request):
    auth_logout(request)
    messages.success( request, '<strong>Success!</strong> You are logged out');
    return HttpResponseRedirect( request.GET.get("next") if request.GET.get("next") else reverse('home') )
